package com.carey.blog.util.jabsorb.serializer;

import org.jabsorb.serializer.AbstractSerializer;
import org.jabsorb.serializer.MarshallException;
import org.jabsorb.serializer.ObjectMatch;
import org.jabsorb.serializer.SerializerState;
import org.jabsorb.serializer.UnmarshallException;

import com.carey.blog.action.StatusCodes;

/**
 * Serializer for {@linkplain StatusCodes}.
 * 
 */
public class StatusCodesSerializer extends AbstractSerializer {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Classes that this can serialise.
	 */
	private static Class<?>[] serializableClasses = new Class<?>[] { StatusCodes.class };
	/**
	 * Classes that this can serialise to.
	 */
	private static Class<?>[] jsonClasses = new Class<?>[] { StatusCodes.class };

	@Override
	public Class<?>[] getJSONClasses() {
		return jsonClasses;
	}

	@Override
	public Class<?>[] getSerializableClasses() {
		return serializableClasses;
	}

	@Override
	public Object marshall(final SerializerState state, final Object p,
			final Object o) throws MarshallException {
		return o;
	}

	@Override
	public ObjectMatch tryUnmarshall(final SerializerState state,
			final Class<?> clazz, final Object jso) throws UnmarshallException {
		final ObjectMatch toReturn;
		if (jso instanceof StatusCodes) {
			toReturn = ObjectMatch.OKAY;
		} else {
			toReturn = ObjectMatch.ROUGHLY_SIMILAR;
		}

		state.setSerialized(jso, toReturn);

		return toReturn;
	}

	@Override
	public Object unmarshall(final SerializerState state, final Class<?> clazz,
			final Object jso) throws UnmarshallException {
		StatusCodes returnValue = null;

		if (jso instanceof Boolean || clazz == StatusCodes.class) {
			returnValue = (StatusCodes) jso;
		}

		state.setSerialized(jso, returnValue);

		return returnValue;
	}
}
